package com.stone.framework.workflow.definition.handler.assignment;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stone.framework.core.exception.BusinessException;
import com.stone.framework.workflow.client.UserClient;

@Component
public class RandomAssignmentHandler extends GenericAssignmentHandler {

    @Autowired
    @Override
    public void setUserClient(UserClient userClient) {
        super.setUserClient(userClient);
    }

    @Override
    protected List<String> handleAssignees(List<String> assignees) {
        if (assignees == null || assignees.isEmpty()) {
            throw new BusinessException("无法获取审批人");
        }
        int random = new Random().ints(0, assignees.size()).findAny().orElse(0);
        String assignee = assignees.get(random);
        return Stream.of(assignee).collect(Collectors.toList());
    }

}
